﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BoutiquePOS.DAL.DAO;
using BoutiquePOS.Manager;

namespace BoutiquePOS.UI.UserUI
{
    public partial class SupplierEntry : System.Web.UI.Page
    {
        private SupplierManager aSupplierManager = new SupplierManager();
        private Supplier aSupplier = new Supplier();

        protected void Page_Load(object sender, EventArgs e)
        {
            LoadGridview();
        }
        public void SaveSupplier(object sender, EventArgs e)
        {
            try
            {
                aSupplierManager = new SupplierManager();
                aSupplier.SupplierName = supplierNameTextBox.Value;
                aSupplier.SupplierAddress = supplierAddressTextBox.Value;
                aSupplier.SupplierPerson = supplierPersonTextBox.Value;
                aSupplier.SupplierCell = supplierCellTextBox.Value;
                aSupplier.SupplierFax = supplierFaxTextBox.Value;
                aSupplier.SupplierEmail = supplierEmailTextBox.Value;
                aSupplier.SupplierType = Convert.ToInt32(supplierTypeSelect.Value);
                aSupplier.SupplierMode = Convert.ToInt32(supplierModeSelect.Value);
                string message = aSupplierManager.SaveSupplier(aSupplier);
                messageResult.InnerText = message;
            }
            catch (Exception exception)
            {
                messageResult.InnerText = "Supplier name can not be saved" + "\n" + exception.ToString();
            }
        }
        public void ClearButton(object sender, EventArgs e)
        {
            Clear();
        }
        public void CloseButton(object sender, EventArgs e)
        {
            Response.Redirect("Home.aspx");
        }
        private void Clear()
        {
            supplierIdTextbox.Value = "";
            supplierNameTextBox.Value = "";
            supplierAddressTextBox.Value = "";
            supplierPersonTextBox.Value = "";
            supplierCellTextBox.Value = "";
            supplierFaxTextBox.Value = "";
            supplierEmailTextBox.Value = "";
            //supplierTypeSelect.Value = "";
            //supplierModeSelect.Value = "";
        }

        public void DeleteButton(object sender, EventArgs e)
        {
            try
            {
                int SupplierId = Convert.ToInt32(supplierIdTextbox.Value);
                SupplierManager supplierManager = new SupplierManager();
                string aa = supplierManager.DeleteSupplier(SupplierId);
                messageResult.InnerText = aa;
            }
            catch (Exception)
            {
                messageResult.InnerText = "Delete Failed";
            }
        }

        private void LoadGridview()
        {
            List<Supplier> aSuppliers;
            aSuppliers = aSupplierManager.GetAllSupplier();
            supplierGridView.DataSource = aSuppliers;
            supplierGridView.DataBind();
        }
        private void LoadGridViewOnSaerch(int i, string supplierIdorName)
        {
            supplierGridView.DataSource = null;
            supplierGridView.DataBind();
            List<Supplier> forms;
            SupplierManager supplierManager = new SupplierManager();
            forms = supplierManager.GetAllSupplierByIdOrName(i, supplierIdorName);
            supplierGridView.DataSource = forms;
            supplierGridView.DataBind();
        }
        public void searchByIdButton_Click(object sender, EventArgs e)
        {
            int i = 0;
            LoadGridViewOnSaerch(i, searchIdTextBox.Value);
        }
        protected void searchByNameButton_Click(object sender, EventArgs e)
        {
            int i = 1;
            LoadGridViewOnSaerch(i, searchNameTextBox.Value);
        }
        protected void supplierGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            supplierGridView.PageIndex = e.NewPageIndex;
            LoadGridview();
        }

    }
}